package chen.bupt.mapreduce.segterm;

import java.io.IOException;
import java.util.List;

import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.ansj.util.recognition.NatureRecognition;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import chen.bupt.constant.Constants;
import chen.bupt.text.StopWordUtil;
import chen.bupt.text.TextClear;
import chen.bupt.text.TextUtils;
import chen.bupt.util.HDFSFileUtils;

/**
 * 分词的MR Job
 * 
 * @author forrest
 * 
 */
public class SegTermJob {
	private static final String inputPath = Constants.SOURCE_INPUT;
	private static final String outputPath = Constants.SEG_PATH;

	public static class Map extends
			Mapper<LongWritable, Text, Text, NullWritable> {

		public void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {
			if (!HDFSFileUtils.isInputFile(context))
				return;
			String str = value.toString();
			String content = null;
			StringBuffer sb = new StringBuffer();
			String keyCont[] = TextUtils.splitTextToCont(str, ";", 7);
			if (keyCont == null || keyCont.length != 2) {
				return;
			} else {
				content = TextClear.clearText(keyCont[1]);
				if (content == null || "".equals(content))
					return;
				List<Term> terms = ToAnalysis.paser(content);
				new NatureRecognition(terms).recognition();
				String term = null;
				for (Term t : terms) {
					if ((term = TextClear.getValidityTerm(t.toString())) != null) {
						if (!StopWordUtil.isStopWord(term)) {
							sb.append(term);
							sb.append(" ");
						}
					}
				}

			}
			context.write(new Text(keyCont[0] + ";" + sb.toString().trim().replaceAll("[ ]+", " ")),
					NullWritable.get());
		}
	}

	public static class Reduce extends
			Reducer<Text, IntWritable, Text, IntWritable> {
		public void reduce(Text key, Iterable<IntWritable> values,
				Context context) throws IOException, InterruptedException {

		}
	}

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		conf.set("mapred.task.timeout", "0");
		Job job = new Job(conf, "WordSplit");
		job.setJarByClass(SegTermJob.class);
		job.setMapperClass(Map.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);
		Path input = new Path(inputPath);
		Path output = new Path(outputPath);
		HDFSFileUtils.deleteFile(output, conf);
		FileInputFormat.addInputPath(job, input);
		FileOutputFormat.setOutputPath(job, output);
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}

}
